package com.hieu.tripmanager;

import java.util.ArrayList;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hieu.bean.CircleChart;
import com.hieu.sql.DAL;
import com.hieu.util.Util;

public class AnalysisActivity extends Activity {

	private static int[] COLORS = new int[] { Color.GREEN, Color.BLUE,
			Color.MAGENTA, Color.CYAN, Color.LTGRAY, Color.GRAY, Color.DKGRAY,
			Color.RED, Color.WHITE, Color.YELLOW };
	private CategorySeries mSeriesPie = new CategorySeries("");
	private DefaultRenderer mRendererPie = new DefaultRenderer();
	private GraphicalView mPieChartView;
	private DAL mDAL;
	private ArrayList<CircleChart> mPieList;
	private ImageView btnNext;
	private TextView txtTotal;
	private TextView txtBalance;
	private TextView txtBudget;
	private long total = 0;
	private Util util = new Util();
	private long budget;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mDAL = new DAL(this);
		budget = mDAL.getSumBudget();
		setContentView(R.layout.analysis_activity_layout);
		txtTotal = (TextView) findViewById(R.id.txtTotal);
		txtBudget = (TextView) findViewById(R.id.txtBudget);
		txtBalance = (TextView) findViewById(R.id.txtBalance);
		btnNext = (ImageView) findViewById(R.id.btnNext);
		btnNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(AnalysisActivity.this,
						AnalysisBarActivity.class);
				startActivity(intent);

			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// Truy xuat CSDL
		mDAL = new DAL(this);
		if (mDAL != null) {
			mPieList = mDAL.getListPieChart();

		}
		boolean check = util.checkIsTrip(AnalysisActivity.this);
		if (check && mPieList != null) {

			if (mPieList != null) {
				// -------------------Pie Chart------------------------------
				mRendererPie.setApplyBackgroundColor(true);
				mRendererPie.setBackgroundColor(Color.argb(100, 50, 50, 50));
				mRendererPie.setLabelsTextSize(18);
				mRendererPie.setLegendTextSize(18);
				mRendererPie.setMargins(new int[] { 20, 30, 15, 0 });
				mRendererPie.setZoomButtonsVisible(true);
				mRendererPie.setLabelsColor(Color.WHITE);
				mRendererPie.setStartAngle(90);

				for (int i = 0; i < mPieList.size(); i++) {
					SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
					if (mPieChartView != null) {
						mSeriesPie.set(
								i,
								mPieList.get(i).getName()
										+ " "
										+ util.convertMoney(mPieList.get(i)
												.getAmount()) + " VNĐ",
								mPieList.get(i).getAmount());
						
						Log.v("name", ""+mPieList.get(i).getName());
						
					} else {
						mSeriesPie.add(
								mPieList.get(i).getName()
										+ " "
										+ util.convertMoney(mPieList.get(i)
												.getAmount()) + " VNĐ",
								mPieList.get(i).getAmount());
//						renderer.setColor(COLORS[(mSeriesPie.getItemCount() - 1)
//													% COLORS.length]);
						Log.v("name", ""+mPieList.get(i).getName());
						Log.v("color", ""+((mSeriesPie.getItemCount() - 1)
								% COLORS.length));
					}
					
					renderer.setColor(COLORS[i]);
					mRendererPie.addSeriesRenderer(renderer);
					total += mPieList.get(i).getAmount();
				}

				txtTotal.setText(util.convertMoney(total) + " VNĐ");
				txtBudget.setText(util.convertMoney(budget) + " VNĐ");
				txtBalance.setText(util.convertMoney(budget - total) + " VNĐ");
				total = 0;
			}

			if (mPieChartView != null) {
				mPieChartView.repaint();
			} else {
				LinearLayout layout = (LinearLayout) findViewById(R.id.circlechart);
				mPieChartView = ChartFactory.getPieChartView(this, mSeriesPie,
						mRendererPie);

				mRendererPie.setClickEnabled(true);
				mRendererPie.setSelectableBuffer(10);

				layout.addView(mPieChartView);

			}

		} else {
			setContentView(R.layout.layout);
			Toast.makeText(getBaseContext(),
					"Hiện tại không có dữ liệu thống kê", Toast.LENGTH_SHORT)
					.show();
		}

	}
}